<?php

function create_data_jarak_tempuh($id =0){
	 
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);

	$view = form_header( "jarak_tempuh" , "jarak_tempuh"  );
 	$fields = my_get_data_by_id('spd_jarak_tempuh','jarak_tempuh_id',$id);
	$label=' ';	
	if(isset( $fields['km'] )){
		$tarifs = my_fetch_array(my_query(
			"SELECT km
			from spd_jarak_tempuh 
			WHERE km> {$fields['km']} 
			ORDER BY km LIMIT 1"
			)) ;
		if( $tarifs['km']>0){
			$label=' - '. rp_format($tarifs['km']).' km' ;
		}			
	}
	
	$km = array(
			'name'=>'km',
			'value'=>(isset($_POST['km'])? $_POST['km'] : $fields['km']),
			'id'=>'km',
			'type'=>'textfield',
			'size'=>'15'
		);
	$form_km = form_dynamic($km);
	$view .= form_field_display( $form_km."&nbsp;".$label , "Jarak"  );
	$tarif = array(
			'name'=>'tarif',
			'value'=>(isset($_POST['tarif'])? $_POST['tarif'] : rp_format($fields['tarif'])),
			'id'=>'tarif',
			'type'=>'textfield',
			'size'=>'25'
		);
	$form_tarif = form_money($tarif); 
	$view .= form_field_display( $form_tarif  , "Tarif"  );
	 
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function create_jarak_tempuh_validate($id){
	$errsubmit = false;
	$err = array(); 

	
	if(! is_numeric($_POST["km"] )  ){
		$errsubmit = true;
		$err[] = "Jarak belum benar";	
	}
	if(! is_numeric($_POST["tarif"] )  ){
		$errsubmit = true;
		$err[] = "Tarif belum benar";	
	}

	$code =  trim($_POST["km"]); 
	$query="SELECT jarak_tempuh_id FROM spd_jarak_tempuh 
		WHERE km='".$code.
		"' AND jarak_tempuh_id<>".$id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Jarak sudah ada";	
	}		
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function create_jarak_tempuh_submit($id = 0){

	$datas = array(
		'km'=>my_type_data_str($_POST['km']),
		'tarif'=>my_type_data_str( set_text_as_number($_POST['tarif'])),
	);
	
	if( $id > 0 ){
		return my_update_record('spd_jarak_tempuh' ,  'jarak_tempuh_id', $id , $datas);
	}
	return my_insert_record( 'spd_jarak_tempuh' , $datas);
}

function daftar_jarak_tempuh_spd(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');

	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Jarak Dari Ibukota Propinsi'=>array('style'=>'text-align:center;border-bottom:2px solid;width:45%'),   
		'Tarif (Rp.)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'text-align:center;border-bottom:2px solid;width:25%'), 
	);

	$query = "SELECT * FROM spd_jarak_tempuh order by km 
	 ";
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&task=edit&id='.$ey['jarak_tempuh_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		 

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['jarak_tempuh_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		
		$label_label_range=get_label_label_range($ey['km'])		;
		
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),  
			'dir'=>  position_text_align(  $label_label_range , 'center'),  
			'nama'=>  position_text_align(  rp_format($ey['tarif'] ), 'right'),  			
			'operasi'=> position_text_align(  $edit_button .' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	return table_builder($header , $datas ,  4, false );
}


function delete_jarak_tempuh($jarak_tempuh_id){
	$query = "DELETE FROM spd_jarak_tempuh WHERE jarak_tempuh_id ={$jarak_tempuh_id} ";
	return my_query($query);
}	

function get_label_label_range($km){

	$tarifs = my_fetch_array(my_query(
		"SELECT km
		from spd_jarak_tempuh 
		WHERE km> {$km} 
		ORDER BY km LIMIT 1"
		)) ;
	if( $tarifs['km']>0){
		return '> '.rp_format($km).' - '. rp_format($tarifs['km']).' km' ;
	}
	return ' > '.rp_format($km).' km' ;
}
